// pages/my/five/five.js
const app = getApp()
Page({

        /**
         * 页面的初始数据
         */
        data: {
                hover: true,

                url: '',
                userinfo: {},
                collectdata: [],
                paycollectdata: [],
                nopaycollectdata: [],
                paypageNum: 1,
                nopaypageNum: 1,
                paytotalpage: 0,
                nopaytotalpage: 0,
                isloading: false,


        },
        yes() {
                this.setData({
                        hover: true,
                        collectdata: this.data.paycollectdata
                })

        },
        no() {
                this.setData({
                        hover: false,
                        collectdata: this.data.nopaycollectdata
                })
        },
        //   获取数据
        getcollectdata(state) {
                return new Promise((resolve, reject) => {
                        this.setData({
                                isloading: true
                        })
                        wx.showLoading({
                                title: "正在加载数据..",
                                mask: true
                        })
                        let t = this
                        let pageNum
                        if (state == 1) {
                                pageNum = this.data.paypageNum;


                        } else {
                                pageNum = this.data.nopaypageNum
                        }
                        wx.request({
                                url: t.data.url + '/usercourse/selectAllUserCourseAndOrder',
                                method: "GET",
                                data: {
                                        userid: t.data.userinfo.id,
                                        pageNum: pageNum,
                                        pageSize: 2,
                                        ispay: state
                                },
                                success(res) {
                                        console.log('res.data->', res.data);
                                        if (res.data.code == 1) {

                                                if (state == 1) {
                                                        t.setData({
                                                                paycollectdata: [...t.data.paycollectdata, ...res.data.data.list],
                                                                paytotalpage: res.data.data.pages

                                                        })
                                                } else {
                                                        t.setData({
                                                                nopaycollectdata: [...t.data.nopaycollectdata, ...res.data.data.list],
                                                                nopaytotalpage: res.data.data.pages
                                                        })
                                                }
                                        } else {
                                                wx.showToast({
                                                        title: '加载数据失败',
                                                })
                                                reject(0)
                                        }


                                },
                                complete() {
                                        wx.hideLoading()
                                        t.setData({
                                                isloading: false
                                        })
                                        wx.stopPullDownRefresh()
                                        resolve(1)
                                }
                        })
                })

        },

        /**
         * 生命周期函数--监听页面加载
         */
        async onLoad(options) {
                let userinfo = wx.getStorageSync('UserInfo')
                console.log('userinfo->', userinfo);
                this.setData({
                        userinfo: userinfo,
                        url: app.globalData.URL
                })
                await this.getcollectdata(1)
                await this.getcollectdata(0)
                console.log('this.data.paycollectdata->', this.data.paycollectdata);
                console.log('this.data.nopaycollectdata->', this.data.nopaycollectdata);
                this.setData({
                        collectdata: this.data.paycollectdata
                })

        },

        /**
         * 生命周期函数--监听页面初次渲染完成
         */
        onReady() {

        },

        /**
         * 生命周期函数--监听页面显示
         */
        onShow() {

        },

        /**
         * 生命周期函数--监听页面隐藏
         */
        onHide() {

        },

        /**
         * 生命周期函数--监听页面卸载
         */
        onUnload() {

        },

        /**
         * 页面相关事件处理函数--监听用户下拉动作
         */
        async onPullDownRefresh() {
                if (this.data.hover) {
                        this.setData({
                                paycollectdata: [],
                                paypageNum: 1,
                                isloading: false
                        })
                        await this.getcollectdata(1)
                        this.setData({
                                collectdata: this.data.paycollectdata
                        })
                } else {
                        this.setData({
                                nopaycollectdata: [],
                                nopaypageNum: 1,
                                isloading: false
                        })
                        await this.getcollectdata(0)
                        this.setData({
                                collectdata: this.data.nopaycollectdata
                        })
                }


        },

        /**
         * 页面上拉触底事件的处理函数
         */
        async onReachBottom() {
                if (this.data.isloading) {
                        return
                }
                if (this.data.hover) {
                        if (this.data.paypageNum >= this.data.paytotalpage) {
                                return
                        }
                        console.log("chufa1");
                        this.setData({
                                paypageNum: this.data.paypageNum + 1
                        })
                        await this.getcollectdata(1)
                        this.setData({
                                collectdata: this.data.paycollectdata
                        })
                } else {
                        if (this.data.nopaypageNum >= this.data.nopaytotalpage) {
                                return
                        }
                        console.log("chufa0");
                        this.setData({
                                nopaypageNum: this.data.nopaypageNum + 1
                        })
                        await this.getcollectdata(0)
                        this.setData({
                                collectdata: this.data.nopaycollectdata
                        })
                }


        },

        /**
         * 用户点击右上角分享
         */
        onShareAppMessage() {

        },
        // 跳转
        todetail(e) {
                console.log('e.currentTarget.dataset->', e.currentTarget.dataset);
                wx.navigateTo({
                        url: '/pages/kecheng/onlinekecheng/kechengdetail/kechengdetail',
                        success: function (res) {
                                // 通过eventChannel向被打开页面传送数据
                                res.eventChannel.emit('onlineDetail', {
                                        id: e.currentTarget.dataset.id,
                                        // typeid: e.currentTarget.dataset.typeid,
                                        // teacherid: e.currentTarget.dataset.teacherid
                                })
                        }
                })
        }
})